from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common import alert
import time
from biz import biz_mysql
Public_platform = ["夜魅小说网","流浪小说"]
Novel_account =[""]
def Inovel_test():
    for s in range(0,2):
        driver = webdriver.Chrome(r'D:\Program Files\python3.63\chromedriver.exe')
        # 进入 inovel.818tu.com
        driver.get("https://inovel.818tu.com/backend/notices")
        driver.maximize_window()
        # driver.set_window_size(0,0)#设定固定大小
        driver.implicitly_wait(15)

        #定位账号
        driver.find_element_by_id('username').clear()
        driver.find_element_by_id('username').send_keys('{0}'.format(Novel_account[s]))
        #定位密码
   
        driver.find_element_by_name('password').clear()
        driver.find_element_by_name('password').send_keys('{0}'.format(''))
        #点击登录
        driver.find_element_by_id('loginbtn').click()
        # print(driver.title)
        # 获取页面cookie
        # cookies = driver.get_cookies()
        time.sleep(2)
        for m in range(2):
            try:
                driver.find_element_by_xpath("//button[text()='我知道了']").click()
                time.sleep(2)
            except Exception:
                print("未处理完弹出框问题")

        # 进入小说列表主页面
        try:
            time.sleep(2)
            driver.find_element_by_xpath('//*[@id="sidebar"]/ul/li[4]//span').click()
        except Exception:
            print("点击小说列表主页面出错")
        #获取页面总页数
        # try:
        #
        # except Exception:
        #     print("获取页面总页数出错")
        time.sleep(1)
        number = driver.find_element_by_xpath('//span[1]/em[2]').text
        number = int(number)


        for x in range(1,number):
        # for x in range(1,17):
            if x == 1:
                y = 28
            elif x == 164:
                y = 3
            else:
                y = 21
            for z in range(1,y):
                #点击跳转框
                try:
                    time.sleep(2)
                    driver.find_element_by_xpath('//span[2]/input').clear()
                    driver.find_element_by_xpath('//span[2]/input').send_keys('{0}'.format(x))
                    driver.find_element_by_xpath('//span[2]/button').click()
                except Exception:
                    print("点击跳转框出错")
                try:
                    time.sleep(2)
                    driver.find_element_by_xpath('//tbody/tr[{}]/td[2]/a'.format(z)).click()
                except Exception:
                    print("点击单个小说页面出错")
                #进入获取小说推广连接页面
                for i in range(1,3):
                    try:
                        # 获取小说名称
                        # time.sleep(2)
                        # Novel_name = driver.find_element_by_xpath('//div[3]/div/div/div/div[1]//h4').text
                        # # print(Novel_name)
                        time.sleep(2)
                        driver.find_element_by_xpath('//li[{}]/span[3]/a'.format(i)).click()
                    except Exception:
                        print("点击小说推广连接页面出错")
                    # all_handles = driver.window_handles
                    # print(all_handles)
                    #清理小说推广信息
                    # 获取小说章节名
                    try:
                        # time.sleep(1)
                        # Chapter = driver.find_element_by_xpath('//p/strong').text
                        time.sleep(2)
                        driver.find_element_by_xpath('//*[@id="create-referral-link-modal"]//div[2]/div/input').clear()
                    except Exception:
                        print("清理小说推广信息出错")
                    #输入派单渠道名称
                    try:
                        driver.find_element_by_xpath('//*[@id="create-referral-link-modal"]//div[2]/div/input').send_keys('{0}'.format(Public_platform[s]))
                    except Exception:
                        print("输入派单渠道名称出错")
                    #选择渠道类型
                    try:
                        time.sleep(1)
                        driver.find_element_by_xpath('//div[3]/div/label[2]/span').click()
                    except Exception:
                        print("选择渠道类型出错")

                    #点击取消
                    # try:
                    #     time.sleep(4)
                    #     driver.find_element_by_xpath('//*[@id="create-referral-link-modal"]//button/span').click()
                    # except Exception:
                    #     print("点击取消出错")
                    #点击生成推广链接
                    try:
                        time.sleep(2)
                        driver.find_element_by_xpath('//*[@id="create-referral-link-modal"]//div[3]/button').click()
                    except Exception:
                        print("点击生成推广链接出错")
                    # 复制链接
                    try:
                        time.sleep(2)
                        url = driver.find_element_by_xpath('//*[@id="get-referral-link-qrcode-modal"]//div[2]/div/div/div[1]/div[1]').text
                        print(url)
                        time.sleep(2)
                        driver.find_element_by_xpath('//*[@id="get-referral-link-qrcode-modal"]//div[3]/button').click()

                        # biz_mysql.insert_novel_mysql(url=url,Novel_name=Novel_name,Chapter=Chapter)

                    except Exception:
                        print("复制链接出错")
                    # try:
                    #     print(Chapter)
                    #     # time.sleep(2)
                    #     print(Novel_name)
                    # except Exception:
                    #     print("获取小说章节名出错")
                    # try:
                    #     biz_mysql.insert_novel_mysql(url=url, Public_platform=Public_platform, Novel_name=Novel_name,Chapter=Chapter)
                    # except Exception:
                    #     print("入库出错")

                    # 点击取消
                    try:
                        time.sleep(2)
                        driver.find_element_by_xpath('//*[@id="get-referral-link-qrcode-modal"]//button/span').click()
                    except Exception:
                        print("点击取消出错")

                    print("打印第{0}页的第{1}部小说的第{2}个推广链接".format(x,z,i))

                print("打印完第{0}页的第{1}个小说推广链接".format(x,z))
                try:
                    time.sleep(2)
                    driver.find_element_by_xpath('//*[@id="sidebar"]/ ul/li[4]//span').click()
                except Exception:
                    print("点击小说列表主页面出错")
            print("打印完第{0}页小说推广链接".format(x))
            try:
                time.sleep(2)
                driver.find_element_by_xpath('//*[@id="sidebar"]/ul/li[4]//span').click()
            except Exception:
                print("点击小说列表主页面出错")
        driver.quit()
if __name__ == "__main__":
    biz_mysql.select_username_password_mysql()
    # driver.quit()































# try:
    # browser.get("https://www.baidu.com")
    # input=browser.find_element_by_id("kw")
    # input.send_keys("Python")
    # input.send_keys(Keys.ENTER)
    # wait=WebDriverWait(browser,10)
    # wait.until(EC.presence_of_element_located((By.ID,"content_left")))
    # print(browser.current_url)
    # print(browser.get_cookies())
    # print(browser.page_source)
    # time.sleep(10)
# finally:
#     browser.close()